package main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Listener implements Runnable {
	private Double velocity;
	BufferedReader reader;
		public Listener(Socket s) throws IOException{
		velocity = 0.0;
		reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
		new Thread(this).start();
		
	}
	@Override
	public void run() {
		while(true){
			String newLine;
			try {
				while((newLine = reader.readLine()) != null){
					interpret(newLine);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	private void interpret(String newLine) {
		String[] commandLine = newLine.split(" ");
		char command = commandLine[0].charAt(0);
		switch(command){
		case 'p':
			elevatorButtonPressed(commandLine[1], commandLine[2]);
			break;
		case 'b':
			floorButtonPressed(commandLine[1], commandLine[2]);
			break;
		case 'v':
			velocityChanged(commandLine[1]);
			break;
		case 'f':
			positionChanged(commandLine[1], commandLine[2]);
		default:
			System.out.println(newLine);
		}
	}
	
	private void floorButtonPressed(String floorString, String directionString) {
		int floorNunmber = Integer.parseInt(floorString);
		int direction = Integer.parseInt(directionString);
		
		System.out.println("floorButtonPressed(" + floorNunmber + ", " + direction + ")");

	}

	private void elevatorButtonPressed(String elevatorString, String floorString) {
		int elevatorNumber = Integer.parseInt(elevatorString);
		int floorNumber = Integer.parseInt(floorString);		
		
		System.out.println("elevatorButtonPressed(" + elevatorNumber + ", " + floorNumber + ")");

	}
	
	private void positionChanged(String elevatorString, String positionString) {
		int elevatorNumber = Integer.parseInt(elevatorString);
		double position = Double.parseDouble(positionString);

	}
	
	private void velocityChanged(String velocityString) {
		this.velocity = Double.parseDouble(velocityString);
	}
}
